<template>
  <div>
    <el-form label-width="100px">
      <el-row type="flex" justify="evenly">
        <el-form-item label="车牌号码:">
          <el-input
            v-model="carNumber"
            type="text"
            placeholder="请输入车牌号码"
            @input="onSearch"
          />
        </el-form-item>

        <el-form-item label="车主姓名">
          <el-input v-model="personName" type="text" placeholder="请输入车主姓名" />
        </el-form-item>

        <el-form-item label="状态">
          <el-select v-model="cardStatus" placeholder="未选择">
            <el-option label="全部" :value="1" />
            <el-option label="可用" :value="2" />
            <el-option label="已过期" :value="3" />
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="onSearch">查询</el-button>
        </el-form-item>
      </el-row>
    </el-form>
    <el-row type="flex" justify="evenly">
      <el-row style="padding-left: 30px;">
        <el-button type="primary" @click="$router.push('/monthCard/addPage')">添加月卡</el-button>
        <el-button type="primary">批量删除</el-button>
      </el-row>
      <el-row type="flex" justify="end">
        <el-alert
          center
          show-icon
          title="本园区共计 1530 个车位，月卡用户 0 人，车位占有率 0.00%"
          type="warning"
        />
      </el-row>
    </el-row>

    <el-table
      v-loading="loading"
      :data="data"
      style="width: 100%"
      @row-click="onBack"
    >
      <el-table-column
        type="selection"
        width="55"
      />
      <el-table-column
        label="序号"
        type="index"
        width="120"
      >
        <template #default="{$index}">
          {{ $index + 1 + 10 *(page -1) }}
        </template>
      </el-table-column>
      <el-table-column
        label="车主名称"
        prop="personName"
        width="120"
      />
      <el-table-column
        prop="phoneNumber"
        label="联系方式"
      />
      <el-table-column
        prop="carNumber"
        label="车牌号码"
      />
      <el-table-column
        prop="carBrand"
        label="车辆品牌"
      />
      <el-table-column
        prop="totalEffectiveDate"
        label="剩余有效天数"
      />
      <el-table-column
        prop="cardStatus"
        label="状态"
      />

      <el-table-column
        prop="demoFlag"
        label="操作"
      >
        <template #default="{row}">
          <el-button type="text" size="small" @click="onSub('renew',row.id)">续费</el-button>
          <el-button type="text" size="small" @click="onSub('look',row.id)">查看</el-button>
          <el-button type="text" size="small" @click="onSub('edit',row.id)">编辑</el-button>
          <el-button type="text" size="small" @click="onSub('del',row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="block" style="float:right">
      <el-pagination
        :current-page="page"
        :page-sizes="[10, 20,50,100]"
        :page-size="10"
        layout="total,prev,pager,next,sizes"
        :total="total"
        @size-change="onSizeChange"
        @current-change="onCurrentChange"
      />

    </div>
  </div>
</template>

<script>
let timeId = null
import { getMonthCardAPI } from '@/api/monthCard'
export default {
  data() {
    return {
      data: [],
      page: 1,
      pageSize: 10,
      total: 0,
      loading: false,
      personName: '',
      carNumber: '',
      cardStatus: ''
    }
  },
  created() {
    this.onLoad()
  },
  methods: {
    onBack(row) {
      this.$refs.formRef.formData = {
        personName: row.personName,
        carNumber: row.carNumber,
        cardStatus: row.cardStatus,
        carBrand: row.carBrand,
        id: row.id

      }
    },
    onSub(type, id) {
      if (type === 'renew') {
      // TODO续费
        this.$router.push('/monthCard/addRenew')
      }
      if (type === 'look') {
      // TODO查看
      }
      if (type === 'edit') {
      // TODO编辑
      }
      if (type === 'del') {
      // TODO删除
        this.$refs.formRef.onDel(id)
      }
    },
    onSearch() {
      clearTimeout(timeId)
      timeId = setTimeout(() => {
        this.onLoad()
      }, 1000)
    },
    async onLoad() {
      this.loading = true
      const { data } = await getMonthCardAPI({
        page: this.page,
        pageSize: this.pageSize,
        carNumber: this.carNumber,
        personName: this.personName,
        cardStatus: this.cardStatus })
      this.loading = false
      this.data = data.rows
      this.total = data.total
    },
    onSizeChange(page) {
      this.pageSize = page
      this.onLoad()

      this.page = 1
    },
    onCurrentChange(page) {
      this.page = page
      this.onLoad()
    }
  }
}
</script>

<style>

</style>
